// An effects plugin for Paint.NET that draws a checkerboard.
// Copyright (C) 2009 Simon Brown
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using PaintDotNet;
using PaintDotNet.Effects;
using PaintDotNet.PropertySystem;
using PaintDotNet.IndirectUI;
using System.IO;
using System.Windows.Forms;

namespace Checkboard
{
	public class Checkboard : PropertyBasedEffect
	{
		public static string StaticName
		{
			get
			{
				return "Checkboard";
			}
		}

		public static Bitmap StaticIcon
		{
			get
			{
				return new Bitmap(16, 16);
			}
		}

		public static string StaticSubMenuName
		{
			get
			{
				return SubmenuNames.Render;
			}
		}

		public enum PropertyNames
		{
			Size = 0
		}

		public Checkboard()
			: base(Checkboard.StaticName, Checkboard.StaticIcon, Checkboard.StaticSubMenuName,
			EffectFlags.Configurable)
		{

		}

		protected override ControlInfo OnCreateConfigUI(PropertyCollection props)
		{
			ControlInfo uiCtrls = PropertyBasedEffect.CreateDefaultConfigUI(props);
			uiCtrls.SetPropertyControlValue(PropertyNames.Size, "Size", "Size");
			return uiCtrls;
		}

		protected override PropertyCollection OnCreatePropertyCollection()
		{
			List<Property> props = new List<Property>();
			props.Add(new Int32Property("Size", 10, 1, 500));
			return new PropertyCollection(props);
		}

		PropertyBasedEffectConfigToken globTok;
		RenderArgs globSrc;
		RenderArgs globDst;

		protected override void OnSetRenderInfo(PropertyBasedEffectConfigToken newToken,
			RenderArgs dstArgs, RenderArgs srcArgs)
		{
			globTok = newToken;
			globSrc = srcArgs;
			globDst = dstArgs;
		}

		protected override void OnRender(Rectangle[] renderRects, int startIndex, int length)
		{
			int size = (int)globTok.GetProperty("Size").Value;

			for (int i = startIndex; i < length; i++)
			{
				Rectangle r = renderRects[i];

				for (int y = r.Top; y < r.Bottom; y++)
				{
					for (int x = r.Left; x < r.Right; x++)
					{
						bool isHorizStep = false, isVertStep = false;

						if ((x / size) % 2 == 0) isHorizStep = true;
						if ((y / size) % 2 == 0) isVertStep = true;

						if ((isHorizStep && !isVertStep) || (!isHorizStep && isVertStep))
							globDst.Surface[x, y] = ColorBgra.FromColor(Color.FromArgb(191, 191, 191));
						else globDst.Surface[x, y] = ColorBgra.White;
					}
				}
			}

		}
	}
}